<template>
  <div>
    <!-- 搜索 -->
    <!-- 故障件运费系数信息 -->
    <!-- table -->
    <TopFilter ref="TopFilters" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :dataSource="dataList" :datakey="'records'" :fetchapi="fetchApi" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange" :is-select-column="false">
        <template slot="controls" slot-scope="props">
            <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        </template>
    </FilterTable>
     <BaseDialog :visible.sync="visibleAdd" :title="'故障件运费系数新增'" destroyOnClose width="50%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
       <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
     </BaseDialog>  
    <BaseDialog :visible.sync="visibleVehicle" :title="'经销商选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="closeVehicle" @save="saveVehicle"></vehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="emitVisible" title="故障件运费系数编辑" width="850px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="emitCloseHandle" @save="emitChange"  :rowData="rowData"></EditForm>
    </BaseDialog>
  </div>
</template>
<script>
import AddForm from './AddForm.vue';
import EditForm from './EditForm.vue';
// import {getOwnerCode} from '@/assets/js/auth';
import vehicle from '@/pages/claimDemage/tools/DealerList.vue';
import { getListByPage, newSave,deleteOne, editSave } from '@/api/claim/factory/PartAndDealer';
export default {
  components: {
   EditForm,
   AddForm,
   vehicle
  },
  data() {
    return {
      rowData: {},
      fetchApi: getListByPage,
      fetchParams: {
            //  ownerCode: getOwnerCode()
        }, //ipa 接口
       // fetchApi: null,
        dataList: [],
        collapse: true,
         emitVisible: false,
        visibleVehicle: false,
        topFilterInit: this.topFilterInitFun(),
        columns: this.creatHeader(),
        visibleAdd: false
    };
  },
  created() {
   this.chuqi();
  },
  methods: {
    
    emitCloseHandle() {
      this.emitVisible = false;
    },
    emitChange() {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    add() {
      this.visibleAdd = true;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    closeVehicle() {
      this.visibleVehicle = false;
    },
    saveVehicle(obj) {
      this.visibleVehicle = false;
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = obj[0].dealerCode;
    },
    async chuqi() {
      // const res = await getOwnerCode();
      if (res.resultCode == 200) {
        this.topFilterInit.find((x) => x.fieldName == 'ownerCode').initialValue = '';
        // this.topFilterInit.find((x) => x.fieldName == 'dealerCode').itemList = res.data.map((y) => ({ text: y.name, value: y.code }));
      }

    },
    edit(val){
       this.rowData = val;
       this.emitVisible = true;
    },
        // 删除
    async delFun(data) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.res = await deleteOne(data.pdId);
          if (this.res.resultCode != 200) {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
          } else {
            this.$notify.success({ title: '提示信息', message: '删除成功!' });
            // 重新加载
            this.fetchParams = { ...this.fetchParams };
          }
        })
      // let res = [];
      // res = await deleteOne({ ptId: data.ptId });
      // if (res.resultCode == 200) {
      //   this.fetchParams = Object.assign({}, this.fetchParams);
      // } else if (res.resultCode == '403') {
      //   console.info('不能删除:{}', res);
      //   this.$notify.error({
      //     title: '失败',
      //     message: res.errMsg
      //   });
      // }
    },

      //选中
    selectChange(rows) {
      this从.selectedRows = rows;
      console.log(this.selectedRows);
    },
    dealerTurnOn() {
      this.visibleVehicle = true;
    },
      //搜索获取数据
    changeHandle(val) {
       //this.fetchParams = Object.assign({},val,{ ownerCode: getOwnerCode() });
        this.fetchParams = { ...val };
      console.log(val);
    },
     //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请输入经销商代码...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.dealerTurnOn()} />
              </div>
            );
          }
        }

      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)} >
                  编辑
                </multiuse-button>
                <el-divider direction="vertical"/>
                <multiuse-button size="mini" type="text" onClick={() => this.delFun(props.row)} >
                  作废
                </multiuse-button>
              </div>
            );
          }
        },
        {title: '经销商代码', dataIndex: 'ownerCode', sorter: true},
        //{title: '经销商名称', dataIndex: 'dealerName', sorter: true},
        {title: '故障件运费系数', dataIndex: 'para', sorter: true},
        
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
